// 畜牧管理
const db = require('../db/index.js');
//导入bcript模块
const bcrypt = require('bcryptjs');
// 获取牧畜列表
exports.getCattleList = async (req, res) => {
    console.log('获取牲畜列表');
    try {
        //查询牧畜编号
        const sql  = 'SELECT imei FROM cattle';
        //执行查询语句
        db.query(sql, (err, rows) => {
            if (err) return res.status(500).json({ code: 0, message: '获取牲畜列表失败' });
            res.json({ code: 1, data: { list: rows } });
        })
      } catch (error) {
        res.status(500).json({ code: 0, message: '获取牲畜列表失败' });
      }
}

// 获取牲畜详细信息
exports.getCattleInfo = async (req, res) => {
  try{
    const  imei  = req.query.imei;
    const sql = `SELECT * FROM cattle WHERE imei = '${imei}'`;
    db.query(sql, (err, results) => {
      if (err) return res.status(500).json({ code: 0, message: '获取牲畜信息失败' });
      if(results.length === 0) return res.status(404).json({ code: 0, message: '牲畜信息不存在' });
      res.json({ code: 1, data: results[0] });
    })
  }
  catch (error) {
    res.status(500).json({ code: 0, message: '获取牲畜信息失败' });
  }
}